package com.tangsm.spring.boot.deferred.result.queue;

import com.tangsm.spring.boot.deferred.result.domain.vo.Result;
import com.tangsm.spring.boot.deferred.result.domain.vo.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 任务队列
 *
 * @author tangsm
 */
@Component
public class TaskQueue {
    private static final Logger log = LoggerFactory.getLogger(TaskQueue.class);
    private static final int QUEUE_LENGTH = 10;
    private final BlockingQueue<Task> queue = new LinkedBlockingDeque<>(QUEUE_LENGTH);
    private int taskId = 0;

    /**
     * 加入任务
     *
     * @param deferredResult DeferredResult异步请求
     */
    public void put(DeferredResult<Result<String>> deferredResult) {
        taskId++;
        log.info("任务加入队列，id为：{}", taskId);
        queue.offer(new Task(taskId, deferredResult));
    }

    /**
     * 获取任务
     *
     * @return 任务实体类
     */
    public Task take() {
        Task task = queue.poll();
        log.info("获得任务:{}", task);
        return task;
    }
}
